package com.ciei.dpagm.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.ciei.dpagm.common.enums.ErrorCodeEnum;
import com.ciei.dpagm.common.service.BaseService;
import com.ciei.dpagm.entity.ChipControlMode;
import com.ciei.dpagm.entity.ChipControlModeInterface;
import com.ciei.dpagm.entity.ChipControlModeInterfaceType;
import com.ciei.dpagm.entity.ChipInterface;
import com.ciei.dpagm.mapper.ChipControlModeInterfaceMapper;
import com.ciei.dpagm.util.APP;
import com.ciei.dpagm.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author pp
 * @date 2023/08/01
 */
@Service
public class ChipControlModeInterfaceService extends BaseService<ChipControlModeInterfaceMapper, ChipControlModeInterface> {

    @Autowired
    private ChipControlModeService chipControlModeService;

    @Autowired
    private ChipControlModeInterfaceTypeService chipControlModeInterfaceTypeService;

    @Autowired
    private ChipInterfaceService chipInterfaceService;

    /**
     * 获取列表
     * @param webData
     * @return
     */
    public JSONObject getList(JSONObject webData){
        Integer driverLibraryId = webData.getInteger("driverLibraryId");
        Integer masterChipId = webData.getInteger("masterChipId");
        String chipControlModeName = webData.getString("chipControlModeName");
        Map<SFunction<ChipControlMode, ?>, Object> fieldValueMap = new HashMap<>();
        fieldValueMap.put(ChipControlMode::getDriverLibraryId,driverLibraryId);
        fieldValueMap.put(ChipControlMode::getName,chipControlModeName);
        List<ChipControlMode> chipControlModeList = chipControlModeService.findByProperties(fieldValueMap);
        if(chipControlModeList.isEmpty()){
            return JsonUtil.getFail(ErrorCodeEnum.DATA_NOT_FOUND,"根据芯片id和控制模式名称找不到对应的控制模式数据，请检查");
        }
        if(chipControlModeList.size() > 1){
            return JsonUtil.getFail(ErrorCodeEnum.DUPLICATE_DATA,"根据芯片id和控制模式名称找到多条控制模式数据，请检查");
        }
        ChipControlMode chipControlMode = chipControlModeList.get(0);
        List<ChipControlModeInterface> chipControlModeInterfaceList = findByProperty(ChipControlModeInterface::getControlModeId, chipControlMode.getControlModeId());
        List<Integer> chipControlModeInterfaceIdList = chipControlModeInterfaceList.stream().map(ChipControlModeInterface::getControlModeInterfaceId).collect(Collectors.toList());
        List<ChipControlModeInterfaceType> chipControlModeInterfaceTypeList = chipControlModeInterfaceTypeService.getList(chipControlModeInterfaceIdList);
        List<String> interfaceTypeList = chipControlModeInterfaceTypeList.stream().map(ChipControlModeInterfaceType::getInterfaceType).collect(Collectors.toList());
        List<ChipInterface> chipInterfaceList = chipInterfaceService.getList(masterChipId, interfaceTypeList);
        List<JSONObject> dataList = new ArrayList<>();
        for(ChipControlModeInterface chipControlModeInterface : chipControlModeInterfaceList){
            List<ChipControlModeInterfaceType> chipControlModeInterfaceTypeListByInterfaceId = chipControlModeInterfaceTypeList.stream().filter(c -> chipControlModeInterface.getControlModeInterfaceId().equals(c.getControlModeInterfaceId())).collect(Collectors.toList());
            List<String> interfaceTypeListByInterfaceId = chipControlModeInterfaceTypeListByInterfaceId.stream().map(ChipControlModeInterfaceType::getInterfaceType).collect(Collectors.toList());
            String slaveChipInterfaceType = String.join(",",interfaceTypeListByInterfaceId);
            JSONObject data = new JSONObject();
            data.fluentPut("slaveChipControlModeInterfaceId",chipControlModeInterface.getControlModeInterfaceId())
                    .fluentPut("slaveChipInterfaceName",chipControlModeInterface.getInterfaceName() == null ? "" : chipControlModeInterface.getInterfaceName())
                    .fluentPut("slaveChipInterfaceDescription",chipControlModeInterface.getInterfaceDescription() == null ? "" : chipControlModeInterface.getInterfaceDescription())
                    .fluentPut("slaveChipInterfaceType",slaveChipInterfaceType);
            List<JSONObject> interfaceTypeOptionList = new ArrayList<>();
            for(ChipControlModeInterfaceType chipControlModeInterfaceType : chipControlModeInterfaceTypeListByInterfaceId){
                JSONObject chipInterfaceOption = new JSONObject();
                List<ChipInterface> chipInterfaceListByInterfaceType = chipInterfaceList.stream().filter(c -> chipControlModeInterfaceType.getInterfaceType().equals(c.getInterfaceType())).collect(Collectors.toList());
                List<JSONObject> resourceNameOptionList = new ArrayList<>();
                for(ChipInterface chipInterface : chipInterfaceListByInterfaceType){
                    JSONObject resourceNameOption = new JSONObject();
                    resourceNameOption.fluentPut("value",chipInterface.getResourceName())
                            .fluentPut("label",chipInterface.getResourceName())
                            .fluentPut("interfaceId",chipInterface.getInterfaceId());
                    resourceNameOptionList.add(resourceNameOption);
                }
                chipInterfaceOption.fluentPut("value",chipControlModeInterfaceType.getInterfaceType())
                        .fluentPut("label",chipControlModeInterfaceType.getInterfaceType())
                        .fluentPut("resourceNameOptionList",resourceNameOptionList);
                interfaceTypeOptionList.add(chipInterfaceOption);
            }
            data.put("interfaceTypeOptionList",interfaceTypeOptionList);
            dataList.add(data);
        }
        return JsonUtil.getSuccess("").fluentPut(APP.DATA,dataList);
    }

    /**
     * 获取列表
     * @param controlModeIdList
     * @return
     */
    public List<ChipControlModeInterface> getList(List<Integer> controlModeIdList){
        if(controlModeIdList.isEmpty()){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<ChipControlModeInterface> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(ChipControlModeInterface::getControlModeId,controlModeIdList);
        return list(lambdaQueryWrapper);
    }
}
